// pages/user/billing/billing.js
let api = require('../../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billList: [],
    currentIndex: 2,
    isShow: 1,
    radioItems: [{
        name: '企业单位',
        value: 2,
        checked: 'true'
      },
      {
        name: '个人/非企业单位',
        value: 1,
        checked: false
      }
    ],
    orderId: "", // 订单id
    isQy: 2,
    totalPrice: 0, // 总价钱
    totalNum: 0, // 数量
    content: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 同步获取每一条数据
    let InvoiceList = wx.getStorageSync("InvoiceList");
    this.setData({
      billList: InvoiceList
    })
    //  数量和总价钱
    let PricendNum = wx.getStorageSync("PricendNum");

    // 如果缓存中没有数据   为空数组  否则   获取  数据
    if (!PricendNum) {
      var totalPrice = 0;
      var totalNum = 0;
    } else {
      var totalPrice = PricendNum[0].totalPrice;
      var arr = []
      for (let i = 0; i < InvoiceList.length; i++) {
        arr.push(InvoiceList[i].id)

      }
      var orderId = arr.toString();
      let totalNum = PricendNum[0].totalNum;
      this.setData({
        totalPrice,
        totalNum,
        orderId
      })
    }
  },
  handelComp(e) {
    var value = e.detail.value;
    if (value == 2) {
      this.setData({
        isQy: 2,
      })
    } else {
      this.setData({
        isQy: 1,
      })
    }
  },
  // 接受方式
  handelChecked(e) {
    console.log(e.detail.value);
    var value = e.detail.value;
    if (value == 1) {
      this.setData({
        isShow: 1
      })
    } else {
      this.setData({
        isShow: 2
      })
    }

  },
  // 提交表单
  billForm: function (e) {
    let bill = e.detail.value;
    let token = wx.getStorageSync('TOKEN');
    // 格式转换
    let billingMethod = parseInt(bill.billingMethod);
    let type = parseInt(bill.type);
    let dutyNum = parseInt(bill.inputNum);
    let newArr = [];
    var obj = {
      "billingMethod": billingMethod,
      "collectAddress": bill.collectAddress,
      "invoiceTitle": bill.inputTitle,
      "type": type,
      "dutyNum": dutyNum
    }
    let {
      billList
    } = this.data;
    for (let i = 0; i < billList.length; i++) {
      newArr[i] = {
        ...billList[i],
        ...obj
      }
    }
    let arr = JSON.stringify(newArr);
    api._post('/omc/invoice/create', arr, token).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '表单提交成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          bill: ""
        });
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: '表单提交失败',
          duration: 2000
        })
      }
    })
    // this.setData({
    //   bill: ""
    // });
    // wx.navigateBack({
    //   delta: 1
    // })
  }
})